<?php
/**
 * [模型]支付
 * 
 * @version $Id: Exchange.php 277 2012-04-24 08:37:00Z linsir123 $
 */
class adminExchange
{
	/**
	 * @var 账户状态
	 */
	public $states = array(
		'0' => '等待支付',
		'1' => '支付成功',
	);
	
	/**
	 * 取得列表
	 */
	public function getList($start, $offset, $params = array())
	{
		$list = D::exchange()->getListWithAccount($start, $offset, $params);
		foreach ($list as $k => $v) 
			$list[$k] = $this->_format($v);
			
		return $list;
	}
	
	/**
	 * 取得数量
	 */
	public function getListCount($params = array())
	{
		return D::exchange()->getListCountWithAccount($params);
	}
	
	/**
	 * 取得一个
	 */
	public function getById($id)
	{
		if (empty($id)) 
			return array();
			
		///
		return D::exchange()->getById($id);
	}
	
	/**
	 * 支付返现
	 * // *、更新账户余额和状态
	 * // *、修改支付状态
	 */
	public function pay($exchage, $account)
	{
		$description = trim($_POST['description']);
		if ( ! empty($exchage) && ! empty($account) && ! empty($description)) {
			$uId = intval($exchage['uid']);
			if (intval($account['state']) == 1) {
				$balance = floatval($account['balance']);
				$money = floatval($exchage['money']);
				if ($balance >= $money) {
					
					///
					D::account()->out($uId, $money);
					D::exchange()->pay($exchage['id'], $description);
					
				} else 
					$message = '申请返现金额大于账户余额,系统有误请检测(严重问题)';
			} else 
				$message = '账户状态不是`申请提现中`';
		} else 
			$message = '数据有误';
			
		///
		if ( ! isset($message)) 
			$message = '支付成功';
		else 
			App::log(array('exchage'=>$exchage, 'account'=>$account), 'pay_error');
			
		return $message;
	}
	
	
	/// 以下为私有方法 ///
	
	
	/**
	 * 格式化
	 */
	private function _format($row)
	{
		$row['state_str'] = $this->states[$row['state']];
		return $row;
	}
}
